package com.dy.树.每个节点的右向指针;

import com.dy.树.TreeLinkNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root==null){return;}
        Queue<TreeLinkNode> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            int count = q.size();
            while(count>1){
                TreeLinkNode tmp = q.poll();
                tmp.next = q.peek();
                if(tmp.left!=null) q.add(tmp.left);
                if(tmp.right!=null) q.add(tmp.right);
                count--;

            }
            TreeLinkNode tmp = q.poll();
            if(tmp.left!=null) q.add(tmp.left);
            if(tmp.right!=null) q.add(tmp.right);
        }
    }
}
